home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d16
/
wininit.arc
/
WININIT.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-08-08
|
4KB
|
176 lines
// wininit.cpp - initialize windows desktop
/* wininit reads the [WinInit] section of WIN.INI, and launches
programs as listed, moving their windows as indicated.
WIN.INI format:
[WinInit]
win1=Show X Y Dx Dy D:\path\name1.exe [args]
win2=Show X Y Dx Dy D:\path\name2.exe [args]
[...]
win1 ... win63 = tags for windows (loaded in numerical order,
NOT in the order found in WIN.INI)
Show = ShowWindow value (1=normal, 2=minimized[=icon])
X,Y = upper left corner coordinates
Dx,Dy = width and height (in pixels)
wininit should be the ONLY program named in window's "run="
and "load=" lines. By putting wininit.exe in the "load=" line,
the program manager window will appear normally; by putting
wininit.exe in the "run=" line, the program manager will appear
as an icon.
This program has no windows - it merely launches the proper
programs and exits.
Written 8/5/91 by Tom Roberts.
Borland C++, ver 2.0. It has no resources or other files.
Copyright 1991 by Tom Roberts. All rights reserved.
*/
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char Progname[] = "WinInit";
const int MAX_NAME = 80;
const int MAX_ARGS = 128;
const int MAX_WINDOWS = 64;
struct ProgInfo {
char name[MAX_NAME];
char args[MAX_ARGS];
int x,y,dx,dy;
int hwnd;
int show;
};
ProgInfo Progs[MAX_WINDOWS];
int Inttok_err = 0;
// inttok() - return integer value of a token
// (sets Inttok_err if error)
int inttok(char *s, char *delim)
{
char *p = strtok(s,delim);
if(p && (isdigit(*p) || *p == '-'))
return atoi(p);
Inttok_err = 1;
return 0;
}
void read_win_ini()
{
char buf[256];
char key[8];
memset(Progs,0,sizeof(Progs));
for(int i=1; i<MAX_WINDOWS; ++i) {
Inttok_err = 0;
wsprintf(key,"win%d",i);
int j = GetProfileString(Progname,key,"",buf,sizeof(buf)-1);
if(j <= 0)
continue;
buf[sizeof(buf)-1] = '\0';
Progs[i].show = inttok(buf," \t\r\n");
Progs[i].x = inttok(NULL," \t\r\n");
Progs[i].y = inttok(NULL," \t\r\n");
Progs[i].dx = inttok(NULL," \t\r\n");
Progs[i].dy = inttok(NULL," \t\r\n");
char *p = strtok(NULL," \t\r\n");
if(p)
strcpy(Progs[i].name,p);
p = strtok(NULL,"\r\n");
if(p)
strcpy(Progs[i].args,p);
if(Inttok_err || Progs[i].name[0] == 0) {
Progs[i].name[0] = 0;
wsprintf(buf,"Error in WIN.INI line for '%s'",(LPSTR)key);
MessageBox(NULL,buf,Progname,MB_APPLMODAL|MB_OK|MB_ICONEXCLAMATION);
}
}
return;
}
int execute(char *program, char *args, int show)
{
char buf[256];
if(program[0] != '\\' && program[1] != ':') {
GetWindowsDirectory(buf,sizeof(buf));
if(buf[strlen(buf)-1] != '\\')
strcat(buf,"\\");
} else {
buf[0] = '\0';
}
strcat(buf,program);
if(args && *args) {
strcat(buf," ");
strcat(buf,args);
}
int retval = WinExec(buf,show);
if(retval < 32) {
wsprintf(buf,"Cannot execute program\r\n'%s'",(LPSTR)program);
MessageBox(NULL,buf,Progname,MB_APPLMODAL|MB_OK|MB_ICONEXCLAMATION);
return 1;
}
return 0;
}
// name2hwnd() - given the program-name, finds the first window's hwnd
HWND name2hwnd(char *name)
{
char buf[MAX_NAME];
HWND hwnd;
hwnd = GetWindow(GetDesktopWindow(),GW_CHILD);
while(hwnd) {
GetModuleFileName(GetClassWord(hwnd,GCW_HMODULE),buf,MAX_NAME);
buf[MAX_NAME-1] = '\0';
int n = strlen(buf) - strlen(name);
if(n >= 0 && stricmp(buf+n,name) == 0)
break;
hwnd = GetNextWindow(hwnd,GW_HWNDNEXT);
}
return hwnd;
}
#pragma argsused
int PASCAL WinMain(HANDLE instance, HANDLE prev, LPSTR cmdline,
int cmdshow)
{
read_win_ini();
// execute the programs
for(int i=1; i<MAX_WINDOWS; ++i) {
if(Progs[i].name[0] == 0)
continue;
if(execute(Progs[i].name,Progs[i].args,SW_HIDE))
continue;
Progs[i].hwnd = name2hwnd(Progs[i].name);
if(Progs[i].hwnd == 0)
continue;
MoveWindow(Progs[i].hwnd,Progs[i].x,Progs[i].y,
Progs[i].dx,Progs[i].dy,TRUE);
ShowWindow(Progs[i].hwnd,Progs[i].show);
}
return 0;
}